package com.csjy.migration.hnnd.workflow;

import com.csjy.migration.hnnd.BusinessTool;
import com.csjy.migration.hnnd.DbUtil;
import com.csjy.migration.hnnd.ProgramArgument;
import com.csjy.migration.hnnd.annotation.Task;
import com.csjy.migration.hnnd.business.NdBpmCounterGuarantee;
import com.csjy.migration.hnnd.business.SourceBusinessData;

import javax.script.ScriptException;

import java.util.*;

/**
 * 更替换反担保措施
 */
public class ItemReplacePledgeHny extends WorkflowMigration {
  private NdBpmCounterGuarantee ndBpmCounterGuarantee = null;
  private Object reviewSecretary = null;

  public ItemReplacePledgeHny(DbUtil sourceDbUtil, DbUtil targetDbUtil, SourceBusinessData sourceData, ProgramArgument programArgument, String projectId, String procInstId, Map procInstRow) throws Exception {
    super(sourceDbUtil, targetDbUtil, sourceData, programArgument, projectId, procInstId, procInstRow);
    this.customInitData();
  }

  @Override
  public void customInitData() throws Exception {
    this.curBizType = "gthfdb";
    this.ndBpmCounterGuarantee = new NdBpmCounterGuarantee(sourceDbUtil,targetDbUtil,sourceData,programArgument,projectId,procInstId);
  }

  @Override
  public void customInsertOrUpdateData() throws Exception {
    this.ndBpmCounterGuarantee.start();
  }

  @Task("置换反担保@A经理提交申请")
  public void ajltjsq() throws Exception {
    this.curAffixType = "dcbg";
    // task1535439045582tjsh B经理提交审核
    this.curModelMap.put("提交审核", new String[]{"task1535439045582tjsh","提交审核"});
    // exclusiveGateway1563504385679tg 办事处负责人审批
    this.curModelMap.put("提交", new String[]{"exclusiveGateway1563504385679tg","提交审核"});
    // exclusiveGateway1575011462928ty 分公司负责人审批
    this.curModelMap.put("提交审批", new String[]{"exclusiveGateway1575011462928ty","提交审核"});
    this.setCommonData(true, null);
    ndBpmCounterGuarantee.getTaskParamMap().put(this.curJbpmTaskInstanceId,  this.initTaskParamMap());
  }

  @Task("置换反担保@B经理提交审核")
  public void bjltjsh() throws Exception {
    this.curAffixType = "bjlshbg";

    // 办事处负责人审批
    this.curModelMap.put("通过", new String[]{"exclusiveGateway1563504385679tg","通过"});
    // 分公司负责人审批
    this.curModelMap.put("同意", new String[]{"exclusiveGateway1563504385679tg","通过"});
    // 部门风控经理审批
    this.curModelMap.put("提交审批", new String[]{"exclusiveGateway1563504385679tg","通过"});

    this.setCommonData(true, null);
  }

  @Task("置换反担保@办事处负责人审批")
  public void bscfzrsp() throws Exception {
    // task1535767462937tg 部门风控经理审批
    this.curModelMap.put("提交审批", new String[]{"task1535767462937tg","通过"});
    // TODO:
    this.curModelMap.put("通过", new String[]{"task1535767462937tg","通过"});
    // TODO:
    this.curModelMap.put("提交融资部", new String[]{"task1535767462937tg","通过"});
    // TODO:
    this.curModelMap.put("同意", new String[]{"task1535767462937tg","通过"});
    // TODO:
    this.curModelMap.put("不通过", new String[]{"endbtg","不通过"});
    this.setCommonData(false, null);
  }

  @Task("项目评审@部门风控经理审批")
  public void bmfkjlsp() throws ScriptException, Exception {
    this.curModelMap.put("同意", new String[]{"exclusiveGateway1575011462928ty","同意"});
    this.curModelMap.put("报批", new String[]{"exclusiveGateway1575011462928ty","同意"});
    this.curModelMap.put("不通过", new String[]{"endbtg","不通过"});
    this.setBmfkjspData();
  }

  @Task("置换反担保@分公司负责人审批")
  public void fgsfzrsp() throws Exception {
    this.curModelMap.put("同意", new String[]{"exclusiveGateway1560992839889xyb","下一步"});
    this.curModelMap.put("通过", new String[]{"exclusiveGateway1560992839889xyb","下一步"});
    this.curModelMap.put("不通过", new String[]{"endbtg","不通过"});
    this.setCommonData(false, null);
  }

  @Task("置换反担保@风控内勤设置D角")
  public void fknqszdj() throws Exception {
    this.curModelMap.put("独立评审", new String[]{"exclusiveGateway1560993302358tjsh","提交审核"});
    this.curModelMap.put("上会审批", new String[]{"exclusiveGateway1560993302358tjsh","提交审核"});
    this.setCommonData(false, null);
    this.actHiVarinstMap.put("nextnode", "task1535447612800");
    this.actHiVarinstMap.put("task1535447612800", "nd_huiqian");

    this.setFknqszdj();
    if(this.curCustomData.get("reviewSecretary") != null){
      this.targetDbUtil.updateByParams("update nd_project_info set REVIEW_SECRETARY=? where id = ?"
              ,Arrays.asList(this.curCustomData.get("reviewSecretary").toString(),this.projectId));
    }
    this.reviewSecretary = this.curCustomData.get("reviewSecretary");
  }

  @Task("置换反担保@独立评审委员投票")
  public void dlpswytp() throws Exception {
    this.curAffixType = "pstp";
    this.curModelMap.put("all", new String[]{"task1535447783517xyb","下一步"});
    this.setCommonData(true, null);
    this.setVoteCustomData();
  }

  @Task("置换反担保@审保秘书汇总意见")
  public void sbmshzyj() throws ScriptException, Exception {
    this.curModelMap.put("all", new String[]{"task1535447855071tj","提交"});
    this.setPsjlData();
    setReviewSecretaryCandidateIdentityLink();
  }

  @Task("置换反担保@独立评审总经理审批签字")
  public void dlpszjlspqz() throws Exception {

    // 独立评审总经理审批签字
    this.curModelMap.put("通过",new String[]{"exclusiveGateway1575254246367tg","通过"});
    // 独立评审董事长审批签字
    this.curModelMap.put("同意",new String[]{"exclusiveGateway1575254246367tg","通过"});
    this.curModelMap.put("不同意",new String[]{"endbty","不同意"});
    this.setCommonData(false,null);
  }

  @Task("置换反担保@独立评审董事长审批签字")
  public void dlpsdszspqz() throws Exception {
    this.dlpszjlspqz();
  }

  @Task("置换反担保@C经理提交风险审核报告")
  public void cjltjfxshbg() throws Exception {
    this.curModelMap.put("通过", new String[]{"task1535439523327tg","通过"});
    this.curModelMap.put("不通过", new String[]{"endbtg","不通过"});
    this.setCommonData(true, null);
  }

  @Task("置换反担保@公司融资部审批")
  public void gsrzbsp() throws Exception {
    this.curModelMap.put("通过", new String[]{"task1535443211707tg","通过"});
    this.curModelMap.put("不通过", new String[]{"endbtg","不通过"});
    this.setCommonData(false, null);

  }

  @Task("置换反担保@林业项目部审批")
  public void lyxmbsp() throws Exception {
    this.gsrzbsp();
  }

  @Task("置换反担保@D经理审核资料")
  public void djlshzl() throws Exception {
    this.curAffixType = "tjfj";
    this.curModelMap.put("通过", new String[]{"task1535443450164tg","通过"});
    this.actHiVarinstMap.put("nextnode","task1535443450164");
    this.actHiVarinstMap.put("task1535443450164","nd_huiqian");
    this.setCommonData(true, null);
    this.setMultipleRoleIdData(true);

  }

  @Task("置换反担保@风控责任人审核")
  public void fkzrrsh() throws Exception {
    this.curModelMap.put("通过", new String[]{"task1535445802227ty","同意"});
    this.curModelMap.put("不通过", new String[]{"endbtg","通过"});
    this.setCommonData(false, null);
  }

  @Task("置换反担保@上会前总经理审核")
  public void shqzjlsh() throws Exception {
    this.curModelMap.put("同意", new String[]{"task1535445846140ty","同意"});
    this.curModelMap.put("不同意", new String[]{"endbty","不同意"});
    this.setCommonData(false, null);
  }

  @Task("置换反担保@评审会主任审核")
  public void pshzrsh() throws Exception {
    this.curModelMap.put("同意", new String[]{"task1535449652421ty","同意"});
    this.curModelMap.put("不同意", new String[]{"endbty","不同意"});
    this.setCommonData(false, null);
  }

  @Task("置换反担保@评审秘书确定评审委员")
  public void psmsqdpswy() throws Exception {
    this.curModelMap.put("只发资料", new String[]{"task1535449654533tjps","提交评审"});
    this.curModelMap.put("提交评审", new String[]{"task1535449654533tjps","提交评审"});
    this.actHiVarinstMap.put("nextnode", "task1535449654533");
    this.actHiVarinstMap.put("task1535449654533", "nd_huiqian");
    this.setCommonData(false, null);
    this.setMultipleRoleIdData(true);
    this.setReviewSecretaryCandidateIdentityLink();
  }

  @Task("置换反担保@评审委员提交意见")
  public void pswytjyj() throws Exception {
    this.curAffixType = "pstp";
    this.curModelMap.put("all", new String[]{"task1535449658028xyb","下一步"});
    this.setCommonData(true, null);
    this.setVoteCustomData();
  }

  @Task("置换反担保@评审秘书提交会议纪要")
  public void psmstjhyjy() throws ScriptException, Exception {
    this.curModelMap.put("all", new String[]{"task1535449233043tj","提交"});
    this.setPsjlData();
    this.setReviewSecretaryCandidateIdentityLink();
  }

  @Task("置换反担保@评审会主任审批签字")
  public void pshzrsp() throws Exception {
    this.curModelMap.put("同意", new String[]{"task1575366065881ty","同意"});
    this.curModelMap.put("不同意", new String[]{"endbty","不同意"});
    this.setCommonData(false, null);
    this.setFinalApprovalData();
  }

  @Task("项目评审@负责人独立审批签字")
  public void fzrdlspqz() throws Exception {
    this.curModelMap.put("同意", new String[]{"task1575366065881ty","同意"});
    this.curModelMap.put("不同意", new String[]{"endbtg","不通过"});
    this.curModelMap.put("退回",new String[]{"task1535449658028th","退回"});
    this.setCommonData(false,null);
    this.setFinalApprovalData();
  }

  /**
   * 设置最终审批结果数据
   */
  public void setFinalApprovalData() {
    Map wfAffixNotionObjWkFlagTwo = this.findFirstWfAffixNotionObjByWkflag(2);
    if (wfAffixNotionObjWkFlagTwo != null) {
      // 同意担保金额
      this.curCustomData.put("applyCoverage", wfAffixNotionObjWkFlagTwo.get("STRESTIMATE"));
      // 项目授信期限
      this.curCustomData.put("applyTime", BusinessTool.convertApplyTimeToMonth(wfAffixNotionObjWkFlagTwo.get("STRRESULT")));
    }
  }

  public void setReviewSecretaryCandidateIdentityLink() {
    if(this.reviewSecretary != null) {
      String[] rsArr = reviewSecretary.toString().split(",");
      if(rsArr.length > 1) {
        for (String reviewSecretarySingle : rsArr) {
          if (this.actHiIdentityLinkMap.get(this.curJbpmTaskInstanceId).get("candidate") == null) {
            this.actHiIdentityLinkMap.get(this.curJbpmTaskInstanceId).put("candidate", reviewSecretarySingle);
          } else {
            this.actHiIdentityLinkMap.get(this.curJbpmTaskInstanceId).put("candidate", reviewSecretarySingle + ","
                    + this.actHiIdentityLinkMap.get(this.curJbpmTaskInstanceId).get("candidate"));
          }
        }
      } else {
        this.actHiIdentityLinkMap.get("process").put("participant", rsArr[0]);
      }
    }
  }


}
